const app = getApp()
var CONFIG = require("../../config/config.js");

Page({
  data: {
    src:'../../images/jia.png',
    jietu:'',
    text: [{ title: '价格不统一'}, { title: '服务态度差'}, { title: '课程顾问不专业'}, { title: '课程顾问口头侮辱'}, { title: '其他方面'}],
    text_index:-1,
    opinion:'',
  },


  uploadImage: function () {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var photoSrc = res.tempFilePaths[0];
        that.setData({
          src: photoSrc,
          jietu: photoSrc
        });
      }
    })
  },

  // 点击投诉那方面
  complaintap:function(e){
    // console.log(e);
    var index = e.target.dataset.index;
    this.setData({
      text_index:index,
      opinion:this.data.text[index],
    })
  },


    // 点击提交
    formSubmit: function (e) { 
        // console.log(this.data.opinion.title);
        // console.log(e);
        var formId = e.detail.formId;
        // console.log(formId);
        var desc = e.detail.value.desc;
        // console.log(desc);
        if(this.data.text_index == -1){
            wx.showToast({
                title: '请选择您要投诉哪方面',
                icon:"none"
            })
        }else if (desc =='') {
            wx.showToast({
                title: '请填写投诉内容',
                icon: 'none'
            })
        return false;
        }else{
            var formData = {
                user_id: app.globalData.uid,
                remark: e.detail.value.desc,
                formId: formId,
                opinion: this.data.opinion.title
            }

            console.log(formData);

            var filePath = this.data.jietu;
            console.log(filePath);

            wx.showLoading({
                title: '提交中',
                mask: true
            })

            if (!filePath) {
                wx.request({
                    url: CONFIG.API_HOST + 'crm/user/addComplainNew',
                    data: formData,
                    method: 'POST',
                    success: function (res) {
                        // console.log(res);
                        wx.hideLoading();
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        })
                        if (res.data.status == 1001 || res.data.status == '1001'){
                            wx.navigateTo({
                                url: '/pages/student/mycomplains',
                            })
                        }
                    },
                    fail: function (res) {
                        wx.hideLoading();
                    }
                })
            }else{
                wx.uploadFile({
                    url: CONFIG.API_HOST + 'crm/user/addComplainNew',
                    filePath: filePath,
                    name: 'jietu',
                    header: {
                        'content-type': 'multipart/form-data'
                    },
                    formData: formData,
                    success: function (res) {
                        // console.log(JSON.parse(res.data));
                        var data = JSON.parse(res.data)
                        wx.hideLoading();
                        wx.showToast({
                            title: data.msg,
                            icon: 'none'
                        })
                        
                        if (data.status == 1001 || data.status == '1001') {
                            wx.navigateTo({
                                url: '/pages/student/mycomplains',
                            })
                        }
                    },
                    fail: function (res) {
                        wx.hideLoading();
                    }
                })
            }
        }
    },

  blurDesc: function (e) {
    var desc = e.detail.value;
    if (!desc) {
      wx.showToast({
        title: '请填写投诉内容',
        icon: 'none',
        duration: 1500
      })
    }
  },
  // 用户点击右上角分享

  onShareAppMessage: function () {
    return {
      title: CONFIG.SHARETITLE,
      path: CONFIG.PATHLOGIN
    }
  }
})